package org.dromara.common.core.enums;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

public enum TimeSqlServerEnum {
   ONEDAY(1, "DATEadd(day, 0,CONVERT(DATETIME,CONVERT(VARCHAR(10),GETDATE(),120))) <=  s.SerSubmitTime"),
   WEEK(2, "DATEadd(day, -7,CONVERT(DATETIME,CONVERT(VARCHAR(10),GETDATE(),120))) <=  s.SerSubmitTime"),
   MONTH(3, "DATEadd(day, -30,CONVERT(DATETIME,CONVERT(VARCHAR(10),GETDATE(),120))) <=  s.SerSubmitTime"),
   YEAR(4, "dateadd(year, datediff(year, 0, getdate()), 0) <=  s.SerSubmitTime"),
   ALL(5, "DATEadd(year, -99,CONVERT(DATETIME,CONVERT(VARCHAR(10),GETDATE(),120)))  <=  s.SerSubmitTime");

   private static final Logger log = LoggerFactory.getLogger(TimeSqlServerEnum.class);
   private Integer code;
   private String type;

   private TimeSqlServerEnum(Integer code, String type) {
      this.code = code;
      this.type = type;
   }

   public Integer getCode() {
      return this.code;
   }

   public String getType() {
      return this.type;
   }

   /** @deprecated */
   @Deprecated
   public static String getTypeByCode(Integer code) {
      TimeSqlServerEnum[] var1 = values();
      int var2 = var1.length;

      for(int var3 = 0; var3 < var2; ++var3) {
         TimeSqlServerEnum conditionType = var1[var3];
         if (code.equals(conditionType.getCode())) {
            return conditionType.getType();
         }
      }

      return null;
   }

   public static TimeSqlServerEnum getByCode(Integer code) {
      return getByCode((Integer)null);
   }

   public static TimeSqlServerEnum getByCode(Integer code, TimeSqlServerEnum defaultVal) {
      TimeSqlServerEnum[] var2 = values();
      int var3 = var2.length;

      for(int var4 = 0; var4 < var3; ++var4) {
         TimeSqlServerEnum loopEnum = var2[var4];
         if (loopEnum.getCode().equals(code)) {
            return loopEnum;
         }
      }

      return defaultVal;
   }

   public static String buildDateSqlByCode(Integer code) {
      TimeSqlServerEnum timeSqlServerEnum = getByCode(code, ALL);
      Date currentTime = new Date();
      Date beginDate;
      switch (timeSqlServerEnum) {
         case ONEDAY:
            beginDate = DateUtils.truncate(currentTime, 5);
            break;
         case WEEK:
            beginDate = DateUtils.addWeeks(DateUtils.truncate(currentTime, 5), -1);
            break;
         case MONTH:
            beginDate = DateUtils.addMonths(DateUtils.truncate(currentTime, 5), -1);
            break;
         case YEAR:
            beginDate = DateUtils.truncate(currentTime, 1);
            break;
         default:
            beginDate = DateUtils.addYears(DateUtils.truncate(currentTime, 5), -99);
      }

      String beginDateText = DateFormatUtils.format(beginDate, "yyyy-MM-dd");
      return buildDateSql(beginDateText, (String)null);
   }

   public static String getYearAndMonth(String year, String month) {
      String time = "year(s.SerSubmitTime) = '" + year + "' and month(s.SerSubmitTime) = '" + month + "'";
      return time;
   }

   /** @deprecated */
   @Deprecated
   public static String getTimeQuantum(String startTime, String endTime) {
      String time = "'" + startTime + " 00:00:00' <= s.SerSubmitTime and s.SerSubmitTime <= '" + endTime + " 23:59:59'";
      return time;
   }

   public static String buildDateSql(String beginDate, String endDate) {
      StringBuilder sb = new StringBuilder();
      boolean beginExists = false;
      if (StringUtils.isNotEmpty(beginDate)) {
         sb.append("s.SerSubmitTime >='").append(beginDate).append(" 00:00:00'");
         beginExists = true;
      }

      if (beginExists && StringUtils.isNotEmpty(endDate)) {
         sb.append(" and ");
      }

      if (StringUtils.isNotEmpty(endDate)) {
         sb.append("s.SerSubmitTime <='").append(endDate).append(" 23:59:59'");
      }

      String result = sb.toString();
      log.info("[buildDateSql]result:{}, startDate:{}, endDate:{}", new Object[]{result, beginDate, endDate});
      return result;
   }

   public static void main(String[] args) {
      buildDateSqlByCode(1);
      buildDateSqlByCode(2);
      buildDateSqlByCode(3);
      buildDateSqlByCode(4);
      buildDateSqlByCode(5);
   }
}
